package com.jk.lesson6;

import com.jk.bean.Dish;
import com.jk.lesson4.MenuTest;

import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Created by Auser on 2016/11/17.
 */
public class lesson6_1 {
    static List<Dish> menu = MenuTest.menu;
    public static void main(String...args){
        test_6_2_1();
        test_6_2_2();
        test_6_2_3();
        test();
        printOut(100);
        test_1();
    }

    /**
     * 归约和汇总
     */
    static void test_6_2_1(){
        Comparator<Dish> dishCaloriesComparator = Comparator.comparingInt(Dish::getCalories).reversed();
        Optional<Dish> mostCalorieDish = menu.stream()
                .collect(Collectors.maxBy(dishCaloriesComparator));
        System.out.println(mostCalorieDish);
    }

    /**
     * 汇总
     */
    static void test_6_2_2(){
        int sumCalories = menu.stream()
                .collect(Collectors.summingInt(Dish::getCalories));
        System.out.println(sumCalories);

        IntSummaryStatistics menuStatistics = menu.stream()
                .collect(Collectors.summarizingInt(Dish::getCalories));
        System.out.println(menuStatistics);
    }

    static void test_6_2_3(){
        String shortMenu = menu.stream()
                .map(Dish::getName)
                .collect(Collectors.joining("===>"));
        System.out.println(shortMenu);
    }

    static void test(){
        System.out.println(f(5));
    }

    private static int f(int x){
        if(x == 0){
            return 0;
        } else {
            return 2 * f(x - 1) + x * x;
        }
    }

    static void printOut(int n){
        if(n >= 10)
            printOut(n / 10);
        printDight(n % 10);
    }

    static void printDight(int n){
        System.out.println(n);
    }

    static void test_1(){
        menu.forEach(System.out::println);
        menu.forEach(dish -> System.out.println(dish));
    }
}
